home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap17 / ChosFont / ChosFont.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  6.8 KB  |  194 lines

  1. /*-----------------------------------------
  2.    CHOSFONT.C -- ChooseFont Demo
  3.                  (c) Charles Petzold, 1998
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  12.                     PSTR szCmdLine, int iCmdShow)
  13. {
  14.      static TCHAR szAppName[] = TEXT ("ChosFont") ;
  15.      HWND         hwnd ;
  16.      MSG          msg ;
  17.      WNDCLASS     wndclass ;
  18.      
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = szAppName ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("ChooseFont"),
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.      
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.      
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.      {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.      }
  51.      return msg.wParam ;
  52. }
  53.  
  54. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  55. {
  56.      static CHOOSEFONT cf ;
  57.      static int        cyChar ;
  58.      static LOGFONT    lf ;
  59.      static TCHAR      szText[] = TEXT ("\x41\x42\x43\x44\x45 ")
  60.                                   TEXT ("\x61\x62\x63\x64\x65 ")
  61.  
  62.                                   TEXT ("\xC0\xC1\xC2\xC3\xC4\xC5 ")
  63.                                   TEXT ("\xE0\xE1\xE2\xE3\xE4\xE5 ") 
  64. #ifdef UNICODE
  65.                                   TEXT ("\x0390\x0391\x0392\x0393\x0394\x0395 ")
  66.                                   TEXT ("\x03B0\x03B1\x03B2\x03B3\x03B4\x03B5 ")
  67.  
  68.                                   TEXT ("\x0410\x0411\x0412\x0413\x0414\x0415 ")
  69.                                   TEXT ("\x0430\x0431\x0432\x0433\x0434\x0435 ")
  70.  
  71.                                   TEXT ("\x5000\x5001\x5002\x5003\x5004") 
  72. #endif
  73.                                  ;
  74.      HDC               hdc ;
  75.      int               y ;
  76.      PAINTSTRUCT       ps ;
  77.      TCHAR             szBuffer [64] ;
  78.      TEXTMETRIC        tm ;
  79.      
  80.      switch (message)
  81.      {
  82.      case WM_CREATE:
  83.  
  84.                // Get text height
  85.  
  86.           cyChar = HIWORD (GetDialogBaseUnits ()) ;
  87.  
  88.                // Initialize the LOGFONT structure
  89.  
  90.           GetObject (GetStockObject (SYSTEM_FONT), sizeof (lf), &lf) ;
  91.  
  92.                // Initialize the CHOOSEFONT structure
  93.  
  94.           cf.lStructSize    = sizeof (CHOOSEFONT) ;
  95.           cf.hwndOwner      = hwnd ;
  96.           cf.hDC            = NULL ;
  97.           cf.lpLogFont      = &lf ;
  98.           cf.iPointSize     = 0 ;
  99.           cf.Flags          = CF_INITTOLOGFONTSTRUCT |
  100.                               CF_SCREENFONTS | CF_EFFECTS ;
  101.           cf.rgbColors      = 0 ;
  102.           cf.lCustData      = 0 ;
  103.           cf.lpfnHook       = NULL ;
  104.           cf.lpTemplateName = NULL ;
  105.           cf.hInstance      = NULL ;
  106.           cf.lpszStyle      = NULL ;
  107.           cf.nFontType      = 0 ;      
  108.           cf.nSizeMin       = 0 ;
  109.           cf.nSizeMax       = 0 ;
  110.           return 0 ;
  111.  
  112.      case WM_COMMAND:
  113.           switch (LOWORD (wParam))
  114.           {
  115.           case IDM_FONT:
  116.                if (ChooseFont (&cf))
  117.                     InvalidateRect (hwnd, NULL, TRUE) ;
  118.                return 0 ;
  119.           }
  120.           return 0 ;
  121.  
  122.      case WM_PAINT:
  123.           hdc = BeginPaint (hwnd, &ps) ;
  124.  
  125.                // Display sample text using selected font
  126.  
  127.           SelectObject (hdc, CreateFontIndirect (&lf)) ;
  128.           GetTextMetrics (hdc, &tm) ;
  129.           SetTextColor (hdc, cf.rgbColors) ;
  130.           TextOut (hdc, 0, y = tm.tmExternalLeading, szText, lstrlen (szText)) ;
  131.  
  132.                // Display LOGFONT structure fields using system font
  133.  
  134.           DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  135.           SetTextColor (hdc, 0) ;
  136.           
  137.           TextOut (hdc, 0, y += tm.tmHeight, szBuffer,
  138.                wsprintf (szBuffer, TEXT ("lfHeight = %i"), lf.lfHeight)) ;
  139.           
  140.           TextOut (hdc, 0, y += cyChar, szBuffer,
  141.                wsprintf (szBuffer, TEXT ("lfWidth = %i"), lf.lfWidth)) ;
  142.           
  143.           TextOut (hdc, 0, y += cyChar, szBuffer,
  144.                wsprintf (szBuffer, TEXT ("lfEscapement = %i"), 
  145.                          lf.lfEscapement)) ;
  146.           
  147.           TextOut (hdc, 0, y += cyChar, szBuffer,
  148.                wsprintf (szBuffer, TEXT ("lfOrientation = %i"), 
  149.                          lf.lfOrientation)) ;
  150.  
  151.           TextOut (hdc, 0, y += cyChar, szBuffer,
  152.                wsprintf (szBuffer, TEXT ("lfWeight = %i"), lf.lfWeight)) ;
  153.  
  154.           TextOut (hdc, 0, y += cyChar, szBuffer,
  155.                wsprintf (szBuffer, TEXT ("lfItalic = %i"), lf.lfItalic)) ;
  156.  
  157.           TextOut (hdc, 0, y += cyChar, szBuffer,
  158.                wsprintf (szBuffer, TEXT ("lfUnderline = %i"), lf.lfUnderline)) ;
  159.  
  160.           TextOut (hdc, 0, y += cyChar, szBuffer,
  161.                wsprintf (szBuffer, TEXT ("lfStrikeOut = %i"), lf.lfStrikeOut)) ;
  162.  
  163.           TextOut (hdc, 0, y += cyChar, szBuffer,
  164.                wsprintf (szBuffer, TEXT ("lfCharSet = %i"), lf.lfCharSet)) ;
  165.  
  166.           TextOut (hdc, 0, y += cyChar, szBuffer,
  167.                wsprintf (szBuffer, TEXT ("lfOutPrecision = %i"), 
  168.                          lf.lfOutPrecision)) ;
  169.  
  170.           TextOut (hdc, 0, y += cyChar, szBuffer,
  171.                wsprintf (szBuffer, TEXT ("lfClipPrecision = %i"), 
  172.                          lf.lfClipPrecision)) ;
  173.  
  174.           TextOut (hdc, 0, y += cyChar, szBuffer,
  175.                wsprintf (szBuffer, TEXT ("lfQuality = %i"), lf.lfQuality)) ;
  176.  
  177.           TextOut (hdc, 0, y += cyChar, szBuffer,
  178.                wsprintf (szBuffer, TEXT ("lfPitchAndFamily = 0x%02X"), 
  179.                          lf.lfPitchAndFamily)) ;
  180.  
  181.           TextOut (hdc, 0, y += cyChar, szBuffer,
  182.                wsprintf (szBuffer, TEXT ("lfFaceName = %s"), lf.lfFaceName)) ;
  183.  
  184.           EndPaint (hwnd, &ps) ;
  185.           return 0 ;
  186.           
  187.      case WM_DESTROY:
  188.           PostQuitMessage (0) ;
  189.           return 0 ;
  190.      }
  191.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  192. }
  193.  
  194.